<template>
  <div>
        <!-- 表单区域 -->
    <el-form :inline="true" ref="supplierFormRef" :model="query">
      <el-form-item prop="name">
        <el-input v-model="query.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item  prop="linkman" v-show="!isDialog">
        <el-input v-model="query.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item  prop="mobile" v-show="!isDialog">
        <el-input v-model="query.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item v-show="!isDialog">
        <el-button type="primary" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="addSupplier">新增</el-button>
        <el-button type="primary" @click="resetSuplierForm('supplierFormRef')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="supplierList" height="380"
       style="width: 100%" border  stripe
       :highlight-current-row="isDialog"
       @current-change="clickCurrentChange"
    >
      <el-table-column type="index" label="*"> </el-table-column>
      <el-table-column prop="name" label="供应商名称"> </el-table-column>
      <el-table-column prop="linkman" label="联系人"> </el-table-column>
      <el-table-column v-show="!isDialog" prop="mobile" label="联系电话"> </el-table-column>
      <el-table-column v-show="!isDialog" prop="remark" label="备注"> </el-table-column>
      <el-table-column label="操作" width="180" v-show="!isDialog">
        <template slot-scope="scope" v-show="!isDialog">
        <el-button size="mini" @click="editSupplier(scope.row.id)">编辑</el-button>
        <el-button size="mini" type="danger" @click="deleteSupplier(scope.row.id)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 20, 30, 40]"
      :page-size="pageSize"
      :layout="isDialog ? 'prev, pager, next' : 'total, sizes, prev, pager, next, jumper'"
      :total="total">
    </el-pagination>
        <!-- 新增供应商对话框 -->
    <el-dialog @close="addSupplierClosed"
      title="新增会员"
      :visible.sync="addSupplierDialogVisible"
      width="50%">
      <el-form ref="addSupplierFormRef" :rules="addSupplierFormRules" :model="addSupplierForm" label-width="100px">
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="addSupplierForm.name"></el-input>
        </el-form-item>
        <el-form-item  prop="linkman" label="联系人">
          <el-input v-model="addSupplierForm.linkman" placeholder="会员姓名"></el-input>
        </el-form-item>
        <el-form-item  prop="mobile" label="手机号码">
          <el-input v-model="addSupplierForm.mobile" placeholder="手机号码"></el-input>
        </el-form-item>
      <el-form-item  prop="remark" label="备注" >
        <el-input type="textarea" v-model="addSupplierForm.remark" placeholder="会员地址"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addSupplierDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddSupplier">确 定</el-button>
      </span>
    </el-dialog>
            <!-- 修改供应商对话框 -->
    <el-dialog @close="updateSupplierClosed"
      title="新增会员"
      :visible.sync="updateSupplierDialogVisible"
      width="50%">
      <el-form ref="updateSupplierFormRef" :rules="updateSupplierFormRules" :model="updateSupplierForm" label-width="100px">
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="updateSupplierForm.name"></el-input>
        </el-form-item>
        <el-form-item  prop="linkman" label="联系人">
          <el-input v-model="updateSupplierForm.linkman" placeholder="会员姓名"></el-input>
        </el-form-item>
        <el-form-item  prop="mobile" label="手机号码">
          <el-input v-model="updateSupplierForm.mobile" placeholder="手机号码"></el-input>
        </el-form-item>
      <el-form-item  prop="remark" label="备注" >
        <el-input type="textarea" v-model="updateSupplierForm.remark" placeholder="会员地址"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateSupplierDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateSupplier">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import supplierApi from '../../api/supplier'
export default {
  data () {
    return {
      // 获取供应商数据
      currentPage: 2,
      pageSize: 5,
      query: {
        name: '',
        linkman: '',
        mobile: ''
      },
      total: 0,
      supplierList: [],
      // 新增对话框
      addSupplierDialogVisible: false,
      addSupplierForm: {
        name: '',
        linkman: '',
        mobile: '',
        remark: ''
      },
      addSupplierFormRules: {
        name: [
          { required: true, message: '请输入卡号', trigger: 'blur' }
        ],
        linkman: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ]
      },
      // 修改对话框
      // 新增对话框
      updateSupplierDialogVisible: false,
      updateSupplierForm: {
        id: null,
        name: '',
        linkman: '',
        mobile: '',
        remark: ''
      },
      updateSupplierFormRules: {
        name: [
          { required: true, message: '请输入卡号', trigger: 'blur' }
        ],
        linkman: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    // 接受父组件传递过来的数据，如果该值为true，则就是父组件的弹窗
    isDialog: Boolean
  },
  created () {
    this.fetchData()
  },
  methods: {
    // 获取供应商数据
    fetchData () {
      supplierApi.getSupplierList(this.currentPage, this.pageSize, this.query).then(response => {
        const resp = response.data
        console.log(resp)
        if (resp.flag) {
          this.total = resp.data.total
          this.supplierList = resp.data.rows
        }
      })
    },
    // 重置按钮
    resetSuplierForm (formNameRef) {
      this.$refs[formNameRef].resetFields()
    },
    // 新增按钮
    addSupplier () {
      this.addSupplierDialogVisible = true
      this.$nextTick(() => {
        this.$refs.addSupplierFormRef.resetFields()
      })
    },
    // 新增对话框的关闭，清空表单(错误的)
    addSupplierClosed () {
      // this.$refs.addSupplierFormRef.resetFields()
    },
    // 点击确定按钮，新增对话框
    submitAddSupplier () {
      this.$refs.addSupplierFormRef.validate(valid => {
        if (valid) {
          console.log(this.addSupplierForm)
          supplierApi.addSupplier(this.addSupplierForm).then(response => {
            const resp = response.data
            console.log(resp)
            if (resp.flag) {
              this.$message.success(resp.message)
              this.addSupplierDialogVisible = false
              this.fetchData()
            } else {
              this.$message.error('新增失败')
            }
          })
        } else {
          return false
        }
      })
    },
    // 修改对话框的关闭，清空对话框(错误的)
    updateSupplierClosed () {
      // this.$refs.updateSupplierFormRef.resetFields()
    },
    // 点击确定按钮，修改对话框
    submitUpdateSupplier () {
      this.$refs.updateSupplierFormRef.validate(valid => {
        if (valid) {
          console.log(this.updateSupplierForm)
          supplierApi.updated(this.updateSupplierForm).then(response => {
            const resp = response.data
            console.log(resp)
            if (resp.flag) {
              this.$message.success(resp.message)
              this.updateSupplierDialogVisible = false
              this.fetchData()
            } else {
              this.$message.error('修改失败')
            }
          })
        } else {
          return false
        }
      })
    },
    // 编辑按钮
    editSupplier (rowId) {
      console.log(rowId)
      this.updateSupplierDialogVisible = true
      // 这里在打开的时候，清空，避免关闭的时候清空表单没有数据
      this.$nextTick(() => {
        this.$refs.updateSupplierFormRef.resetFields()
      })
      supplierApi.getById(rowId).then(response => {
        const resp = response.data
        if (resp.flag) {
          this.updateSupplierForm = resp.data
        } else {
          this.$message.error('获取信息失败')
        }
      })
    },
    // 删除按钮
    deleteSupplier (rowId) {
      console.log(rowId)
      this.$confirm('此操作将永久删除该供应商, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        supplierApi.deleteById(rowId).then(response => {
          const resp = response.data
          if (resp.flag) {
            this.$message.success(resp.message)
            this.fetchData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(error => {
        this.$message.info('已取消删除')
        return error
      })
    },
    // 分页功能
    handleSizeChange (newSize) {
      this.pageSize = newSize
      this.fetchData()
    },
    handleCurrentChange (newPage) {
      this.currentPage = newPage
      this.fetchData()
    },
    // 当点击表格某一行的时候，调用这个函数
    clickCurrentChange (currentRow) {
      // 将这一行数据传入父组件
      console.log(currentRow)
      this.$emit('chooseRow', currentRow)
    }
  }
}
</script>
<style scoped>
.el-form{
  margin-top: 20px
}
</style>
